package com.sun.dream.fish.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * Description:
 * Created By ljl on 2023/12/19.
 */
public class RandomNameUtil {
    public static String getRandomJianHan(int len) {

        StringBuilder ret = new StringBuilder();

        for (int i = 0; i < len; i++) {

            String str = null;

            int hightPos, lowPos; // 定义高低位

            Random random = new Random();

            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值

            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值

            byte[] b = new byte[2];

            b[0] = (Integer.valueOf(hightPos).byteValue());

            b[1] = (Integer.valueOf(lowPos).byteValue());

            try {
                str = new String(b, "GBK"); // 转成中文

            } catch (UnsupportedEncodingException ex) {

                ex.printStackTrace();

            }
            ret.append(str);

        }

        return ret.toString();

    }

    //生成随机用户名，数字和字母组成,

    public static String getStringRandom(int len) {

        StringBuilder val = new StringBuilder();

        Random random = new Random();

        //参数length，表示生成几位随机数

        for (int i = 0; i < len; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";

            //输出字母还是数字

            if ("char".equalsIgnoreCase(charOrNum)) {

                //输出是大写字母还是小写字母

                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;

                val.append((char) (random.nextInt(26) + temp));

            } else {

                val.append(String.valueOf(random.nextInt(10)));

            }

        }

        return val.toString();

    }


    public static String getCombinedRandom(int limitMax) {
        Random random = new Random();
        int temp = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while (limitMax != temp) {
            limitMax -= temp;
            temp = random.nextInt(limitMax);
            if (temp == 0) {
                stringBuilder.append(randomSelect(limitMax - temp));
                break;
            }
            stringBuilder.append(randomSelect(temp));
        }
        return stringBuilder.toString();
    }

    public static String getCombinedRandom() {
        Random random = new Random();
        return getCombinedRandom(random.nextInt(4, 15));
    }

    public static void main(String[] args) {
        System.out.println(getCombinedRandom(10));
    }

    public static String randomSelect(int len) {
        Random random = new Random();
        int num = random.nextInt(2);
        return num == 1 ? getRandomJianHan(len) : getStringRandom(len);
    }
}
